﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UI.Core;
using BO;
namespace UI
{
    public partial class DailyReport : TextFormBase, IMenuForm
    {
        public DailyReport()
        {
            InitializeComponent();
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            reportDate.DateTimePicker.Value = DateTime.Now;

        }
        protected override void OnQuery()
        {

            var billiingList = ListFactory<BillingList>.GetList("Billing.HasDeleted=0 and Billing.IsConfirm=1");
            var today = reportDate.DateTimePicker.Value;
            var qYear = from item in billiingList
                        where item.TransDate.Year == today.Year
                        group item by item.CompanyName into g
                        select new { Company = g.Key, Amount = g.Sum(item => item.Amount), };
            var qMonth = from item in billiingList
                        where item.TransDate.Month == today.Month
                           && item.TransDate.Year == today.Year
                        group item by item.CompanyName into g
                        select new { Company = g.Key, Amount = g.Sum(item => item.Amount), };
            var qDay = from item in billiingList
                         where item.TransDate.Date == today.Date
                         group item by item.CompanyName into g
                         select new { Company = g.Key, Amount = g.Sum(item => item.Amount), };

            string str = string.Empty;
            double d, m, y;
            d = m = y = 0;
            var companys = ListFactory<CompanyList>.GetList("");
            string TotalCompany = "全国";
            string endStr = string.Empty;
            #region 第一遍
            foreach (var item in companys)
            {
                double amt = 0;
                var ditem = qDay.Where(a => a.Company == item.Name).FirstOrDefault();
                if (ditem != null)
                {
                    amt = ditem.Amount;
                }
                str += string.Format("{0}日总:{1,13:0.0},{2}", item.Name, amt, endStr);
                d += amt;
                amt = 0;
                var mitem = qMonth.Where(a => a.Company == item.Name).FirstOrDefault();
                if (mitem != null)
                {
                    amt = mitem.Amount;
                  
                }
                str += string.Format("{0}月总:{1,13:0.0},{2}", item.Name, amt, endStr);
                m += amt;
                amt = 0;
                var yitem = qYear.Where(a => a.Company == item.Name).FirstOrDefault();
                if (yitem != null)
                {
                    amt = yitem.Amount;
                  
                }
                str += string.Format("{0}全年总:{1,13:0.0},{2}", item.Name, amt, endStr);
                y += amt;
                str += Environment.NewLine;
              
            }
           
            str += string.Format("{0}日总:{1,13:0.0},{2}", TotalCompany, d, endStr);
            str += string.Format("{0}月总:{1,13:0.0},{2}", TotalCompany, m, endStr);
            str += string.Format("{0}全年总:{1,13:0.0},{2}", TotalCompany, y, endStr);
            str += Environment.NewLine;
            #endregion
            MyText.Text = str;
            str = string.Empty + Environment.NewLine +Environment.NewLine; 
            endStr = Environment.NewLine;
            d = m = y = 0;
            #region 第二遍
            foreach (var item in companys)
            {
                double amt = 0;
                var ditem = qDay.Where(a => a.Company == item.Name).FirstOrDefault();
                if (ditem != null)
                {
                    amt = ditem.Amount;
                }
                str += string.Format("{0}日总:{1},{2}", item.Name, amt, endStr);
                d += amt;
                amt = 0;
                var mitem = qMonth.Where(a => a.Company == item.Name).FirstOrDefault();
                if (mitem != null)
                {
                    amt = mitem.Amount;

                }
                str += string.Format("{0}月总:{1},{2}", item.Name, amt, endStr);
                m += amt;
                amt = 0;
                var yitem = qYear.Where(a => a.Company == item.Name).FirstOrDefault();
                if (yitem != null)
                {
                    amt = yitem.Amount;

                }
                str += string.Format("{0}全年总:{1},{2}", item.Name, amt, endStr);
                y += amt;
                str += Environment.NewLine;

            }
          
            str += string.Format("{0}日总:{1},{2}", TotalCompany, d, endStr);
            str += string.Format("{0}月总:{1},{2}", TotalCompany, m, endStr);
            str += string.Format("{0}全年总:{1},{2}", TotalCompany, y, endStr);
            str += Environment.NewLine;
            #endregion
            MyText.Text += str;
        }
    }
}
